package com.unipay.springcloud20.controller;

import com.unipay.springcloud20.entities.CommonResult;
import com.unipay.springcloud20.entities.Payment;
import com.unipay.springcloud20.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Project: springcloud20
 * @Author: C.yu
 * @Time: 2023-03-14 21:38:41
 * @Desc: TODO
 */
@Slf4j
@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    //@PostMapping(value = "/payment/add")
    //public CommonResult add(@RequestBody Payment payment){
    //    int add = paymentService.add(payment);
    //    log.info("插入结果：" + add);
    //    log.info(payment.toString());
    //    if (add > 0){
    //        return new CommonResult(200, "插入成功！", add);
    //    }else {
    //        return new CommonResult(404, "插入失败。", null);
    //    }
    //}
    @PostMapping(value = "/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment)
    {
        int result = paymentService.create(payment);
        log.info("***插入结果***："+result);
        log.info("参数payment： " + payment);

        if(result > 0)
        {
            return new CommonResult(200,"插入数据库成功,serverPort: " , result + "，参数：" + payment);
        }else{
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping(value = "/payment/getPayByID/{id}")
    public CommonResult getPayByID(@PathVariable("id") Long id){
        Payment payment = paymentService.getPayByID(id);
        if (payment == null){
            return new CommonResult(404, "查询失败。", null);
        }else {
            return new CommonResult(200, "查询成功！", payment);
        }
    }
}
